package cn.da.shuai.cool.common.entity;

import cn.da.shuai.cool.common.constant.Const;
import cn.da.shuai.cool.common.view.BasicView;
import com.fasterxml.jackson.annotation.JsonView;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class R<T> {

    public static final int SUCCESS_CODE = 200;
    public static final int ERROR_CODE = 500;

    @JsonView(BasicView.class)
    private int code;
    @JsonView(BasicView.class)
    private String msg;
    @JsonView(BasicView.class)
    private T data;

    public R(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public R(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> R<T> ok(String msg, T data) {
        return new R<>(SUCCESS_CODE, msg, data);
    }

    public static <T> R<T> ok() {
        return new R<>(SUCCESS_CODE, Const.SUCCESS);
    }

    public static <T> R<T> ok(T data) {
        return new R<>(SUCCESS_CODE, Const.SUCCESS, data);
    }

    public static <T> R<T> error() {
        return new R<>(ERROR_CODE, Const.ERROR);
    }

    public static <T> R<T> error(int code, String msg) {
        return new R<>(code, msg);
    }

    public static <T> R<T> error(String msg) {
        return new R<>(ERROR_CODE, msg);
    }

}
